package cn.ulyer.common.utils;


import org.apache.commons.lang3.StringUtils;

/**
 * 脱敏工具类
 * @athor : ulyer
 * @date: 2021/10/27
 */
public class SensitiveUtil {

    final static String SENSITIVE_STR = "*";

    /**
     * 邮箱脱敏  1234556@qq.com  to   1***@qq.com
     * @param email
     * @return
     */
    public static String email(String email){
        if(StringUtils.isBlank(email)){
            return "";
        }
        int atIndex = email.indexOf("@");
        if(atIndex<=1){
            return email;
        }
        return StringUtils.rightPad(StringUtils.left(email,1),4,SENSITIVE_STR).concat(StringUtils.substring(email,atIndex));
    }


    /**
     * 只显示姓
     * @param chineseName
     * @return
     */
    public static String chineseName(String chineseName){
        if(StringUtils.isBlank(chineseName)){
            return "";
        }
        return StringUtils.rightPad(StringUtils.left(chineseName,1),StringUtils.length(chineseName),SENSITIVE_STR);
    }

    /**
     * 手机号脱敏  139****1234
     * @param mobilePhone
     * @return
     */
    public static String mobilePhone(String mobilePhone){
        if(StringUtils.isBlank(mobilePhone)){
            return "";
        }
        return StringUtils.rightPad(StringUtils.left(mobilePhone,3),7,SENSITIVE_STR).concat(StringUtils.right(mobilePhone,4));
    }

    /**
     * 固定电话   保留后四位
     * @param fixedPhone
     * @return
     */
    public static String fixedPhone(String fixedPhone){
        if(StringUtils.isBlank(fixedPhone)){
            return "";
        }
        return StringUtils.rightPad("",4,SENSITIVE_STR).concat(StringUtils.right(fixedPhone,4));
    }


    /**
     * 前三位和后四位保留
     * @param idCard
     * @return
     */
    public static String idCard(String idCard){
        if(StringUtils.isBlank(idCard)){
            return "";
        }
        return StringUtils.left(idCard,3).concat(StringUtils.leftPad(StringUtils.right(idCard,4),StringUtils.length(idCard)-3,SENSITIVE_STR));
    }

    /**
     * 前6位和后四位保留
     * @param bankCard
     * @return
     */
    public static String bankCard(String bankCard){
        if(StringUtils.isBlank(bankCard)){
            return "";
        }
        return StringUtils.left(bankCard,6).concat(StringUtils.leftPad(StringUtils.right(bankCard,4),StringUtils.length(bankCard)-6,SENSITIVE_STR));
    }

    public static String address(String address,int savedLength){
        int length = StringUtils.length(address);
        if(length<=savedLength){
            return address;
        }
        return StringUtils.rightPad(StringUtils.left(address,savedLength),length,SENSITIVE_STR);
    }


}
